/*
(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423   
(new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18  
 */
/********************** date工具类 ***************/
Date.prototype.format = function(format){
    var o = {
        "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(), //day
        "h+" : this.getHours(), //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3), //quarter
        "S" : this.getMilliseconds() //millisecond
    }
    if(/(y+)/.test(format)) format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4- RegExp.$1.length));
    for(var k in o)if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,RegExp.$1.length==1? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
};
/*
*date
* months:要添加的月数()
* return:返回结果,Date
*/
Date.prototype.addMonth = function (months) {
    var y = this.getFullYear();
    var m = this.getMonth();
    var d = this.getDate();
    y += Math.floor((m + months) / 12); //计算年
    m = Math.floor((m + months) % 12) + 1; //计算月
    if(m<=0){
        m += 12;
    }
    var d_max = new Date(y + "/" + (m+1)+ "/0").getDate();  //获取计算后的月的最大天数
    if (d > d_max) {
        d = d_max;
    }
    return new Date(y + "-" + (m < 10 ? ("0" + m) : m) + "-" + (d < 10 ? ("0" + d) : d));
};
/********************** 公共工具类 ***************/
var PublicUtil ={
    isNotEmpty:  function(val){
        return !this.isEmpty(val);
    },
    isEmpty: function(val){
        if ((val==null || typeof(val)=="undefined")|| (typeof(val)=="string"&&val==""&&val!="undefined")){
           return true;
        }else{
            return false;
        }
    },
    isDebug: function(){
        if(this.isNotEmpty(configDebug)&&configDebug=="true"){
            return true;
        }else{
            return false;
        }
    },
    //去除元素内所有内容 strIds："#id1,#id2,#id3"
    emptyHtml: function(strIds){
        try{
            var ids = strIds.trim(",").split(",");
            $(ids).each(function(){
                var obj = $(this.toString());
                if(obj.length>0){
                    $(obj).each(function(){
                        $(this).html("");
                    });
                }else{
                    obj.html("");
                }
            });
        }catch(ex){
            if(PublicUtil.isDebug()){
                throw new Error("js方法：【PublicUtil.emptyHtml(strIds)】，error！");
            }
        }
    },
    //去除元素的值 strIds："#id1,#id2,#id3"
    emptyValue: function(strIds){
        try{
            var ids = strIds.trim(",").split(",");
            $(ids).each(function(){
                var obj = $(this.toString());
                if(obj.length>0){
                    $(obj).each(function(){
                        $(this).val("");
                    });
                }else{
                    obj.val("");
                }
            });
        }catch(ex){
            if(PublicUtil.isDebug()){
                throw new Error("js方法：【PublicUtil.emptyValue(strIds)】，error！");
            }
        }
    },
    //去除Textarea内所有内容 strIds："#id1,#id2,#id3"
    emptyTextarea: function(strIds){
        try{
            var ids = strIds.trim(",").split(",");
            $(ids).each(function(){
                var obj = $(this.toString());
                if(obj.length>0){
                    $(obj).each(function(){
                        $(this).empty();
                        $(this).val("");
                    });
                }else{
                    obj.empty();
                    obj.val("");
                }
            });
        }catch(ex){
            if(PublicUtil.isDebug()){
                throw new Error("js方法：【PublicUtil.emptyTextarea(strIds)】，error！");
            }
        }
    },
    //小数相加精度保留
    numAdd:function (num1, num2) {//要相加的两个数
        var baseNum, baseNum1, baseNum2;
        try {
            baseNum1 = num1.toString().split(".")[1].length;
        }
        catch (e) {
            baseNum1 = 0;
        }
        try {
            baseNum2 = num2.toString().split(".")[1].length;
        }
        catch (e) {
            baseNum2 = 0;
        }
        baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
        return (num1 * baseNum + num2 * baseNum) / baseNum;
    }
}
 /*
 String工具类
字符串的拼接一定使用StringBuffer来拼接，否则容易造成浏览器卡顿或内存溢出。特别是针对一些执行js效率不高的浏览器！！
*/
/********************** String工具类***************/
//trim去掉字符串两边的指定字符,默去空格
String.prototype.trim = function(tag) {
    if (!tag) {
        tag = '\\s';
    }else {
        if (tag == '\\') {
        tag = '\\\\';
    } else if (tag == ',' || tag == '|' || tag == ';') {
            tag = '\\' + tag;
        }else {
            tag = '\\s';
        }
    }
    eval('var reg=/(^' + tag + '+)|(' + tag + '+$)/g;');
    return this.replace(reg, '');
};
//字符串截取后面加入...
String.prototype.interceptString = function(len) {
    if (this.length > len) {
        return this.substring(0, len) + "...";
    } else {
        return this;
    }
}
//将一个字符串用给定的字符变成数组
String.prototype.toArray = function(tag) {
    if (this.indexOf(tag) != -1) {
        return this.split(tag);
    }else {
        if (this != '') {
            return [this.toString()];
        }else {
            return [];
        }
    }
}
//只留下数字(0123456789)
String.prototype.toNumber= function() {
    return this.replace(/\D/g, "");
}
//保留中文
String.prototype.toCN= function() {
    var regEx = /[^\u4e00-\u9fa5\uf900-\ufa2d]/g;
    return this.replace(regEx, '');
}
//转成int
String.prototype.toInt= function() {
    var temp = this.replace(/\D/g, "");
    return isNaN(parseInt(temp)) ? this.toString() : parseInt(temp);
}
//是否是以XX开头
String.prototype.startsWith= function(tag){
    return this.substring(0, tag.length) == tag;
}
//是否已XX结尾
String.prototype.endWith= function(tag){
    return this.substring(this.length - tag.length) == tag;
}
//StringBuffer
var StringBuffer = function() {
    this._strs = new Array;
};
StringBuffer.prototype.append = function (str) {
    this._strs.push(str);
};
StringBuffer.prototype.toString = function() {
    return this._strs.join("");
};
String.prototype.replaceAll = function(s1,s2){
    return this.replace(new RegExp(s1,"gm"),s2);
}
 // Array工具类
/********************** Arry ***************/
//根据数据取得再数组中的索引
Array.prototype.getIndex = function(obj){
    for (var i = 0; i < this.length; i++) {
        if (obj == this[i]) {
            return i;
        }
    }
    return -1;
}
//移除数组中的某元素
Array.prototype.remove= function (obj) {
    for (var i = 0; i < this.length; i++) {
        if (obj == this[i]) {
            this.splice(i, 1);
            break;
        }
    }
    return this;
}
//判断元素是否在数组中
Array.prototype.contains= function (obj) {
    for (var i = 0; i < this.length; i++) {
        if (obj == this[i]) {
            return true;
        }
    }
    return false;
}
 // 浏览器相关操作
/********************** 浏览器相关操作 ***************/
//进入全屏模式,  判断各种浏览器，找到正确的方法
var launchFullScreen = function (element) {
  if(element.requestFullscreen) {
    element.requestFullscreen();
  } else if(element.mozRequestFullScreen) {
    element.mozRequestFullScreen();
  } else if(element.webkitRequestFullscreen) {
    element.webkitRequestFullscreen();
  } else if(element.msRequestFullscreen) {
    element.msRequestFullscreen();
  }
  return true;
}
//退出全屏模式
var exitFullScreen = function () {
  if(document.exitFullscreen) {
    document.exitFullscreen();
  } else if(document.mozCancelFullScreen) {
    document.mozCancelFullScreen();
  } else if(document.webkitExitFullscreen) {
    document.webkitExitFullscreen();
  }
  return false;
}
//cookie操作
var CookieUtil={
    path: "/",
    domain: 'demo.j2ee.com',
    add: function(name,val){
        $.cookie(name, val, {expires: 7, path: this.path, domain: this.domain, secure: true});
    },
    remove: function(name){
        $.cookie(name, null,{path: this.path, domain: this.domain});
    },
    get: function(name){
         $.cookie(name,{path: this.path, domain: this.domain});
    }
}
//error
var error={
     e_404: function(){
         alertMessage("404","未找到改页面！","warning");
     },
     e_500: function(){
         alertMessage("500","服务器内部错误！","error");
     },
     e_403: function(){
         alertMessage("403","权限不足！","warning");
     }
}
/*
Url request
 */
function UrlRequest(paras){
    var url = location.href;
    var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");
    var paraObj = {}
    for (i=0; j=paraString[i]; i++){
        paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);
    }
    var returnValue = paraObj[paras.toLowerCase()];
    if(typeof(returnValue)=="undefined"){
        return "";
    }else{
        return returnValue;
    }
}
function getExplorer() {
    var explorer = window.navigator.userAgent;
    //ie
    if (explorer.indexOf("MSIE") >= 0) {
        return 'ie';
    }
        //firefox
    else if (explorer.indexOf("Firefox") >= 0) {
        return 'Firefox';
    }
        //Chrome
    else if (explorer.indexOf("Chrome") >= 0) {
        return 'Chrome';
    }
        //Opera
    else if (explorer.indexOf("Opera") >= 0) {
        return 'Opera';
    }
        //Safari
    else if (explorer.indexOf("Safari") >= 0) {
        return 'Safari';
    }
};

//====================正则限制=========================
// ----------------------------------------------------------------------
// <summary>
// 限制只能输入数字
// $(".onlyNumAlpha").onlyNum();
// </summary>
// ----------------------------------------------------------------------
$.fn.onlyNum = function () {
    $(this).keypress(function (event) {
        var eventObj = event || e;
        var keyCode = eventObj.keyCode || eventObj.which;
        if ((keyCode >= 48 && keyCode <= 57))
            return true;
        else
            return false;
    }).focus(function () {
        //禁用输入法
        this.style.imeMode = 'disabled';
    }).bind("paste", function (e) {
        var clipboardData = window.clipboardData; //for IE
        if (!clipboardData) { // for chrome
            clipboardData = e.originalEvent.clipboardData;
        }
        //获取剪切板的内容
        var clipboard = clipboardData.getData("Text");
        if (/^\d+$/.test(clipboard))
            return true;
        else
            return false;
    });
};// ----------------------------------------------------------------------
// <summary>
// 限制只能输入字母
// $(".onlyNumAlpha").onlyAlpha();
// </summary>
// ----------------------------------------------------------------------
$.fn.onlyAlpha = function () {
    $(this).keypress(function (event) {
        var eventObj = event || e;
        var keyCode = eventObj.keyCode || eventObj.which;
        if ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122))
            return true;
        else
            return false;
    }).focus(function () {
        this.style.imeMode = 'disabled';
    }).bind("paste", function (e) {
        var clipboardData = window.clipboardData; //for IE
        if (!clipboardData) { // for chrome
            clipboardData = e.originalEvent.clipboardData;
        }
        //获取剪切板的内容
        var clipboard = clipboardData.getData("Text");
        if (/^[a-zA-Z]+$/.test(clipboard))
            return true;
        else
            return false;
    });
};// ----------------------------------------------------------------------
// <summary>
// 限制只能输入数字和字母
// $(".onlyNumAlpha").onlyNumAlpha();
// </summary>
// ----------------------------------------------------------------------
$.fn.onlyNumAlpha = function () {
    $(this).keypress(function (event) {
        var eventObj = event || e;
        var keyCode = eventObj.keyCode || eventObj.which;
        if (keyCode==46 || (keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122))
            return true;
        else
            return false;
    }).focus(function () {
        this.style.imeMode = 'disabled';
    }).bind("paste", function (e) {
        var clipboardData = window.clipboardData; //for IE
        if (!clipboardData) { // for chrome
            clipboardData = e.originalEvent.clipboardData;
        }
        //获取剪切板的内容
        var clipboard = clipboardData.getData("Text");
        if (/^(\d|[a-zA-Z])+$/.test(clipboard))
            return true;
        else
            return false;
    });
};